//package cn.zn.wx.config;
//
//import cn.zn.wx.security.vo.MyUserDetails;
//import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
//import org.apache.ibatis.reflection.MetaObject;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.stereotype.Component;
//
//import java.time.LocalDateTime;
//
//@Component
//public class MybatisPlusMetaObjectConfig implements MetaObjectHandler {
//
//    @Override
//    public void insertFill(MetaObject metaObject) {
//        this.strictInsertFill(metaObject, "version", Integer.class, 0);
//        this.strictInsertFill(metaObject, "sort", Integer.class, 100);
//        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
//        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
//        if (this.getFieldValByName("isActive", metaObject) == null) {
//            this.strictInsertFill(metaObject, "isActive", Boolean.class, true);
//        }
//        if (this.getFieldValByName("orderNum", metaObject) == null) {
//            this.strictInsertFill(metaObject, "orderNum", Integer.class, 100);
//        }
//        Authentication authentication = SecurityContextHolder.getContext()
//                .getAuthentication();
//        Object principal = authentication.getPrincipal();
//        if (principal != null && !principal.equals("anonymousUser")) {
//            MyUserDetails myUserDetails = (MyUserDetails) principal;
//            this.strictInsertFill(metaObject, "createUser", Long.class, myUserDetails.getUserId());
//            this.strictInsertFill(metaObject, "updateUser", Long.class, myUserDetails.getUserId());
//        } else {
//            this.strictInsertFill(metaObject, "createUser", Long.class, 1L);
//            this.strictInsertFill(metaObject, "updateUser", Long.class, 1L);
//        }
//    }
//
//    @Override
//    public void updateFill(MetaObject metaObject) {
//        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
//        Authentication authentication = SecurityContextHolder.getContext()
//                .getAuthentication();
//        Object principal = authentication.getPrincipal();
//        if (principal != null && !principal.equals("anonymousUser")) {
//            MyUserDetails myUserDetails = (MyUserDetails) principal;
//            this.strictUpdateFill(metaObject, "updateUser", Long.class, myUserDetails.getUserId());
//        } else {
//            this.strictUpdateFill(metaObject, "updateUser", Long.class, 1L);
//        }
//    }
//}
